package com.liu.servlet;

import com.liu.entity.T_user;
import com.liu.service.user.T_userService;
import com.liu.service.user.T_userServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PipedWriter;
import java.io.PrintWriter;

@WebServlet(name = "DengluServlet", urlPatterns = "/DengluServlet")
public class DengluServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        T_userService service =new T_userServiceImpl();
        String accout = request.getParameter("account");
        String password = request.getParameter("password");
//        String [] cunchu = request.getParameterValues("cunchu");
//
//        if (cunchu!=null){
//            Cookie cookie=new Cookie("pwd",password);
//            Cookie cookie1=new Cookie("out",accout);
//            cookie.setMaxAge(60*60);
//            response.addCookie(cookie);
//            response.addCookie(cookie1);
//        }else {
//            Cookie [] cookies=request.getCookies();
//            for (Cookie tmp:cookies){
//                if (tmp.getName().equals("pwd")){
//                    tmp.setMaxAge(0);
//                    response.addCookie(tmp);
//                }
//                if (tmp.getName().equals("out")){
//                    tmp.setMaxAge(0);
//                    response.addCookie(tmp);
//                }
//            }
//        }
        T_user user=service.denglu(accout,password);
        PrintWriter out=response.getWriter();

        if (user!=null){
            request.getSession().setAttribute("user",user);
            out.print("<script>\n" +
                    "      alert(\"登入成功!\");\n" +
                    "      location.href='client/Index.jsp';\n" +
                    "    </script>");
        }else {
            out.print("<script>\n" +
                    "      alert(\"登入失败!\");\n" +
                    "history.back();    \n" +
                    "    </script>");
        }

    }
}
